package com.principalservice.controller;

import annotation.DisableBaseResponse;
import com.principalservice.service.HolidayService;
import com.principalservice.service.UserInfoService;
import generator.support.ResponseResult;
import generator.vo.likeHoliday;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/holi")
@CrossOrigin
@Slf4j
public class HolidayController {

    @Autowired
    HolidayService holidayService;

    @Autowired
    UserInfoService userInfoService;

    /**
     * 分页查询请假信息，一页10条
     * @param page 页数
     * @return 分页信息
     */
    @GetMapping("/getAll")
    public List<Map<String, Object>> getAllHoliday(){
        log.info("HolidayController -> getAllHoliday");
        return holidayService.getAllHoliday();
    }

    /**
     * 对请假信息的模糊查询
     */
    @PostMapping("/like")
    public List<Map<String, Object>> likeHoliday(@RequestBody likeHoliday likeHoliday){
        log.info("HolidayController -> likeHoliday");
        return holidayService.likeHoliday(likeHoliday);
    }

    /**
     * 添加请假信息
     */
    @PostMapping("/add")
    @DisableBaseResponse
    public ResponseResult<Object> addHoliday(@RequestBody likeHoliday likeHoliday){
        log.info("HolidayController -> addHoliday");
        return holidayService.addHoliday(likeHoliday);
    }

    /**
     * 删除请假信息
     */
    @GetMapping("/delete/{id}")
    @DisableBaseResponse
    public ResponseResult<Object> deleteHoli(@PathVariable String id){
        try{
            holidayService.removeById(Integer.parseInt(id));
        } catch (NumberFormatException e){
            return new ResponseResult<>(400, "请选择正确的ID");
        }
        return new ResponseResult<>();
    }

    @PostMapping("/edit")
    @DisableBaseResponse
    public ResponseResult<Object> editHoli(@RequestBody likeHoliday likeHoliday){
        return holidayService.editHoli(likeHoliday);
    }
}
